package com.jht.bletool2java.characteristic.ftms;


import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;


import com.jht.bletool2java.characteristic.TranslateData;
import com.jht.bletool2java.util.ByteUtil;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002ad7-0000-1000-8000-00805f9b34fb")
public class SupportedHeartRateRange implements TranslateData {
    private final String TAG = "SupportedHeartRateRange";
    private static SupportedHeartRateRange data;

    private byte[] MinimumHeartRate = new byte[1];
    private byte[] MaximumHeartRate = new byte[1];
    private byte[] MinimumIncrement = new byte[1];
    private byte[] valueData;
    private boolean isInvalidData = false;
    public static SupportedHeartRateRange getInstance() {
        data = new SupportedHeartRateRange();
        return data;
    }

    public SupportedHeartRateRange() {
    }
    public SupportedHeartRateRange(BluetoothGattCharacteristic characteristic){
        byte[] value = characteristic.getValue();
        valueData = value;
        parseData(value);
        for(int i=0;i<value.length;i++){
            Log.i(TAG, "SupportedHeartRateRange: i = " + i +"; ox" + Integer.toHexString(ByteUtil.byte1ToInt(value[i])));
        }
    }
    public void parseData(byte[] buffer) {
        System.arraycopy(buffer, 0, MinimumHeartRate, 0, 1);
        System.arraycopy(buffer, 1, MaximumHeartRate, 0, 1);
        System.arraycopy(buffer, 2, MinimumIncrement, 0, 1);
    }

    public int getMinimumHeartRate() {
        return ByteUtil.byte1ToInt(MinimumHeartRate[0]);
    }

    public int getMaximumHeartRate() {
        return ByteUtil.byte1ToInt(MaximumHeartRate[0]);
    }

    public int getMinimumIncrement() {
        return ByteUtil.byte1ToInt(MinimumIncrement[0]);
    }

    @Override
    public String convert2String() {
        return "SupportedHeartRateRange:\nMinimumHeartRate : " + getMinimumHeartRate()
                + " bpm \n MaximumHeartRate : " + getMaximumHeartRate()
                + " bpm \n MinimumIncrement : " + getMinimumIncrement() + " bpm "+"\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }
    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return valueData;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = (value == null || value.length != 3);
        if (isInvalidData) {
            //非法数据
            init();
            valueData = value;
        } else {
            valueData = value;
            parseData(value);
            for(int i=0;i<value.length;i++){
                Log.i(TAG, "SupportedHeartRateRange: i = " + i +"; ox" + Integer.toHexString(ByteUtil.byte1ToInt(value[i])));
            }
        }
    }

    private void init(){
        MinimumHeartRate = new byte[1];
        MaximumHeartRate = new byte[1];
        MinimumIncrement = new byte[1];
        valueData = null;
    }
}
